<?php

namespace App\Admin\Eloquent;

class Article extends Model
{
    /**
     * 文章标签CodeType
     */
    const ARTICLE_TAG='article_tags';

    protected $fillable = ['image', 'title', 'published_at','end_at','author', 'display', 'article_category_id', 'recommended', 'article_type', 'tags', 'sort', 'content', 'description'];

    /**
     * 查询文章类型
     *
     * @return ArticleCategory[]
     */
    public static function get_article_categories(){
        return ArticleCategory::all();
    }

    /**
     * 查询所有文章标签
     *
     * @return Code[]
     */
    public static function get_tags(){
        return Code::getByTypeId(self::ARTICLE_TAG);
    }

    /**
     * 根据title和article_category_id模糊查询文章列表
     *
     * @param array $args
     * @param int $pagesize
     * @return mixed
     */
    public static function paging_query(array $args,int $pagesize = 15){
        $query = Article::select('articles.id', 'articles.title', 'articles.display', 'articles.published_at', 'articles.article_type', 'articles.recommended', 'b.name as category_name')
            ->leftjoin('article_categories as b', 'b.id', '=', 'articles.article_category_id');

        if (isset($args['title']) && !empty($args['title']))
            $query = $query->where('articles.title', 'like', '%' . $args['title'] . '%');

        if (isset($args['article_category_id']) &&!empty($args['article_category_id'])&&$args['article_category_id']!='xx')
            $query = $query->where('b.id', $args['article_category_id']);

        $articles = $query->orderBy('articles.created_at', 'desc')->paginate($pagesize);

        return $articles;
    }
    /**
     * 发送一封项目公告
     * 
     * @param  int    $projectId [description]
     * @return [type]            [description]
     */
    public function saveProjectNotice(int $projectId){
    }
}